<?php

class Model_ServiceCentral extends Model_Service
{

	public function loginCheck($dpt_id, $dpt_account_login, $password="")
	{			
		$select  = $this->db->select()->from('acc_client');
		$select->where('email = ?', $dpt_account_login);
		
		$user = $this->db->fetchRow($select);
		//Zend_Debug::dump($user);
		$passwd = md5($password);
				
		if (!$user)
		{
			$rs['status'] = false;
			$rs['message'] = 'account not found';
		}
		else
		{
		   	$status = $user['status'];
         	$passwd_stored = $user['password'];
           
			if ($status=='Disable' or $status=='Pending' or $status=='Suspend')
	     	{
	        	$rs['status'] = false;
            	$rs['message'] = 'Account is not verified, <a href="/signup/resend/email/'.$dpt_account_login.'">click here to email activation link again.</a>';
	     	}
	     	elseif ($password && $passwd !=$passwd_stored)
	    	{
	        	$rs['status'] = false;
            	$rs['message'] = 'wrong password';
	     	}
	     	else
	     	{
	        	$rs['status'] = true;
            	$rs['dpt_account_id'] = $user['client_id'];
            	$rs['dpt_account_login'] = $dpt_account_login;
           	 	$rs['email'] = $user['email'];
            	
            	$rs['login_area'] = 'central';
            
	     	}    
		}

		return $rs; // return OK if passed
	}
	
	public function linkedAccounts($client_id)
	{
		$linked = array($client_id);
		
		$select  = $this->db->select()->from('acc_client_link', array('client_id', 'linked_client_id'));
		$select->where('client_id = ?', $client_id);
		$select->orWhere('linked_client_id = ?', $client_id);
		//$select->group('client_id');
		
		$ids = $this->db->fetchAll($select);
		foreach ($ids as $id)
		{
			array_push($linked, $id['client_id']);
			array_push($linked, $id['linked_client_id']);
		}

		$ids = array_unique($linked);
		$where = "client_id = '" .implode(" ' or client_id = '",$ids) ."'";
		$orwhere = "linked_client_id = '" .implode(" ' or linked_client_id = '",$ids) ."'";
		
		$query = "select client_id, linked_client_id from acc_client_link where ".$where. " or ".$orwhere ;

		$ids_more = $this->db->fetchAll($query);
		foreach ($ids_more as $id_more)
		{
			array_push($linked, $id_more['client_id']);
			array_push($linked, $id_more['linked_client_id']);
		}

		$ids_more = array_unique($linked);
		
		$where = "client_id = '" .implode(" ' or client_id = '",$ids_more) ."'";
		$query = "select client_id, email from acc_client where ".$where;

		$accounts[$client_id] = $this->db->fetchPairs($query);

		return $accounts;
	}

	
	public function addLinkedAccount($client_id,$email,$password)
	{
		$passwd = md5($password);
		$select  = $this->db->select()->from('acc_client');
		$select->where('email = ?', $email);
		$select->where('password = ?', $passwd);
		$client = $this->db->fetchRow($select);
		//Zend_Debug::dump($client);exit;
		
		if ($client){
			
			$data = array(
            	'client_id' => $client_id,
            	'linked_client_id' => $client['client_id']
            );
            try {
            	$this->db->insert('acc_client_link',$data);
				return true;
            }catch (Zend_Exception $e){
            	return false;
            }
		}else{
			return false;
		}
		
	}
	
	
	public function addGoogleLinkedAccount($client_id,$google_email)
	{
		$select  = $this->db->select()->from('acc_client');
		$select->where('email = ?', $google_email);
		$client = $this->db->fetchRow($select);
		//Zend_Debug::dump($client);exit;
		
		if ($client){
			
			$data = array(
            	'client_id' => $client_id,
            	'linked_client_id' => $client['client_id']
            );
            try {
            	$this->db->insert('acc_client_link',$data);
				return true;
            }catch (Zend_Exception $e){
            	return false;
            }
		}else{
			return false;
		}		
	}
	
	
	public function removeLinkedAccount($user_id,$client_id)
	{
		$sql = " delete from acc_client_link where ( client_id='$user_id' and linked_client_id='$client_id' ) or ( linked_client_id='$user_id' and client_id='$client_id' ) ";
		$this->db->query($sql);		
		return true;
	}
	
	
}